import 'dart:convert';

import 'package:shared_preferences/shared_preferences.dart';

import '../const/store_const.dart';
import '../type/user_type.dart';

class StoreUtil {
  static Future<SharedPreferences> prefs = SharedPreferences.getInstance();

  static String token = '';
  static int userId = 0;
  static UserInfoType userInfo = UserInfoType();

  static void setAccount(String accountInfo) async {
    SharedPreferences prefs = await StoreUtil.prefs;
    prefs.setString(account, accountInfo);
  }

  static Future<AccountType?> getAccount() async {
    SharedPreferences prefs = await StoreUtil.prefs;
    String? accountInfo = prefs.getString(account);
    if (null != accountInfo) {
      Map<String,dynamic> map = jsonDecode(accountInfo);
      return AccountType(employeeNum: map['employeeNum'], pwd: map['pwd']);
    }
    return null;
  }

  static void setToken(String token) {
    StoreUtil.token = token;
  }

  static String getToken() {
    return StoreUtil.token;
  }

  static void setUserId(int userId) {
    StoreUtil.userId = userId;
  }

  static int getUserId() {
    return StoreUtil.userId;
  }

  static void setUserInfo(UserInfoType userInfo) {
    StoreUtil.userInfo = userInfo;
  }

  static UserInfoType getUserInfo() {
    return StoreUtil.userInfo;
  }

  static void clear(){
    StoreUtil.token = '';
    StoreUtil.userId = 0;
    StoreUtil.userInfo = UserInfoType();
  }
}
